<template>
  <el-aside :width="width">
    <el-menu
      background-color="#545c64"
      text-color="#fff"
      :collapse="isCollapse"
      :collapse-transition="false"
      :default-active="activeMenu"
    >
      <!-- 展开状态 -->
      <h3 v-show="!isCollapse">问政江西登录平台</h3>
      <!-- 折叠状态 -->
      <h3 v-show="isCollapse">问政</h3>
      <!-- 没有子菜单的遍历 -->
      <el-menu-item
        v-for="item in noChildren"
        :index="item.path"
        :key="item.path"
        @click="changeMenu(item)"
      >
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>
      <!-- 有子菜单的遍历 -->
      <el-sub-menu
        v-for="item in hasChildren"
        :index="item.path"
        :key="item.path"
      >
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
            v-for="(subItem, subIndex) in item.children"
            :index="subItem.path"
            :key="subItem.path"
            @click="changeMenu(subItem)"
          >
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script setup>
import { ref, computed } from "vue";
import { useAllDataStore } from "@/stores";
import { useRouter, useRoute } from "vue-router";

const router = useRouter();
//获取当前路由
const route = useRoute();
//当前激活的菜单
const activeMenu = computed(() => route.path);

//路由跳转
const changeMenu = (item) => {
  router.push(item.path);
  store.selectMenu(item);
};

const store = useAllDataStore();
const isCollapse = computed(() => store.state.isCollapse);
//width
const width = computed(() => (store.state.isCollapse ? "64px" : "180px"));
//获取路由菜单
const menuData = computed(() => store.state.routerList);
//没有子菜单
const noChildren = computed(
  () =>
    menuData.value?.filter(
      (item) => !item.children || item.children.length === 0
    ) || []
);
//有子菜单
const hasChildren = computed(
  () =>
    menuData.value?.filter(
      (item) => item.children && item.children.length !== 0
    ) || []
);
</script>

<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.el-menu {
  border-right: none;
  h3 {
    margin: 0;
    line-height: 48px;
    color: #fff;
    text-align: center;
    font-size: 16px;
  }
}
.el-aside {
  height: 100%;
  background-color: #545c64;
}
</style>
